<?php
namespace app\home\controller;
use think\Db;
use clt\Lunar;
use think\facade\Env;
class Index extends Common{
    public function initialize(){
        parent::initialize();
    }
    public function index(){
        $order = input('order','createtime');
        $list=Db::name('article')->alias('a')
            ->join(config('database.prefix').'category c','a.catid = c.id','left')
            ->field('a.*,c.catdir,c.catname')
            ->order($order.' desc')
            ->limit('15')
            ->select();
        foreach ($list as $k=>$v){
            $list[$k]['time'] = toDate($v['createtime']);
            $list[$k]['url'] = url('home/'.$v['catdir'].'/info',array('id'=>$v['id'],'catId'=>$v['catid']));
        }
        $this->assign('list', $list);

        $listAll = cache('listAll');
        if(!$listAll){
            $listAll = Db::name('ad')->where('open',1)->whereIn('as_id',[1,3,4,5])->order('sort asc')->select();
            cache('listAll', $listAll, 3600);
        }
        $adList = [];
        $lcList = [];
        $adListKb = [];
        $adListYs = [];
        foreach($listAll as $val){
            if($val['as_id'] == 1){
                $adList[]= $val;
            }
            if($val['as_id'] == 3){
                $adListYs[]= $val;
            }
            if($val['as_id'] == 4){
                $adListKb[]= $val;
            }
            if($val['as_id'] == 5){
                $lcList[]= $val;
            }
        }
        //轮播图
        $this->assign('adList', $adList);
        //回收流程
        $this->assign('lcList', $lcList);
        //专业金银回收，以用户需求为中心
        $this->assign('adListKb', $adListKb);
        //三大优势
        $this->assign('adListYs', $adListYs);


        $catlist = db('category')->field('id,catname,catdir,parentdir,pid')->where('pid','=',4)->order('sort asc')->select();
        $this->assign('catlist',$catlist);


        $map['posid'] = 1;
        $map['catid'] = 1;
        $newlist = db('article')->where($map)->order('sort asc,createtime desc')->limit('6')->select();
        $this->assign('newlist',$newlist);

        //回收项目
        $hslist = db('picture')->where([['posid','=',1]])->order('sort asc,createtime desc')->limit('9')->select();
        //print_r($hslist);die;
        $this->assign('hslist',$hslist);


        //洪辉动态
        $hhlist = db('article')->where([['posid','=',1],['catid','eq',41]])->order('sort asc,createtime desc')->limit('11')->select();
        $this->assign('hhlist',$hhlist);


        //行业动态
        $hylist = db('article')->where([['posid','=',1],['catid','eq',42]])->order('sort asc,createtime desc')->limit('11')->select();
        $this->assign('hylist',$hylist);

        //常见问题
        $cjlist = db('article')->where([['posid','=',1],['catid','eq',43]])->order('sort asc,createtime desc')->limit('11')->select();
        $this->assign('cjlist',$cjlist);


        //关于我们和练习我们
        $data = Db::name('page')->where(['status'=>1])->limit('2')->select();
        $aboutInfo =$data[0];
        $this->assign('aboutInfo', $aboutInfo);


        return $this->fetch();
    }
    public function download($id=''){
        $map['id'] = $id;
        $files = Db::name('download')->where($map)->find();
        return download(Env::get('root_path').'public'.$files['files'], $files['title']);
    }

    public function search(){
        //搜索article表、product表、picture表
        $search_name = input('search_name');
        $search = ['query'=>[]];
        $search['query']['search_name'] = $search_name;
        $article = db('article')->where('title|content','like',"%{$search_name}%")->select();
        $product = db('product')->where('title','like',"%{$search_name}%")->select();

        $searlist = array_merge($article,$product);

        $this -> assign('searlist',$searlist);
        $this->assign('search_name',$search_name);

        return $this->fetch();
    }
}